<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="vue-loader简单学习"><meta name="keywords" content="vue loader"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>vue-loader简单学习 | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#vue-loader"><span class="toc-number">1.</span> <span class="toc-text">vue-loader</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%80%E8%A6%81%E4%BB%8B%E7%BB%8D%E4%B8%80%E4%B8%8B"><span class="toc-number">1.1.</span> <span class="toc-text">简要介绍一下</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#webpack-%E9%85%8D%E7%BD%AE"><span class="toc-number">1.2.</span> <span class="toc-text">webpack 配置</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#url%E4%BC%9A%E8%A2%AB%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.3.</span> <span class="toc-text">url会被转换</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BE%8B%E5%A6%82%EF%BC%8C%E4%B8%8B%E9%9D%A2%E7%9A%84%E6%A8%A1%E6%9D%BF%E4%BB%A3%E7%A0%81%E7%89%87%E6%AE%B5%EF%BC%9A"><span class="toc-number">1.3.1.</span> <span class="toc-text">例如，下面的模板代码片段：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88"><span class="toc-number">1.3.2.</span> <span class="toc-text">为什么</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E9%A2%84%E5%A4%84%E7%90%86%E5%99%A8"><span class="toc-number">1.4.</span> <span class="toc-text">使用预处理器</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#Sass"><span class="toc-number">1.4.1.</span> <span class="toc-text">Sass</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#PostCSS"><span class="toc-number">1.4.2.</span> <span class="toc-text">PostCSS</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Babel"><span class="toc-number">1.4.3.</span> <span class="toc-text">Babel</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Scoped-CSS"><span class="toc-number">1.5.</span> <span class="toc-text">Scoped CSS</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%90%E7%BB%84%E4%BB%B6%E7%9A%84%E6%A0%B9%E5%85%83%E7%B4%A0"><span class="toc-number">1.5.1.</span> <span class="toc-text">子组件的根元素</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#CSS-Modules"><span class="toc-number">1.6.</span> <span class="toc-text">CSS Modules</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%83%AD%E9%87%8D%E8%BD%BD"><span class="toc-number">1.7.</span> <span class="toc-text">热重载</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%8A%B6%E6%80%81%E4%BF%9D%E7%95%99%E8%A7%84%E5%88%99"><span class="toc-number">1.7.1.</span> <span class="toc-text">状态保留规则</span></a></li></ol></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">vue-loader简单学习</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2022-03-03</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">2.1k</span><span class="post-meta__separator">|</span><span>阅读时长: 7 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><h2 id="vue-loader"><a href="#vue-loader" class="headerlink" title="vue-loader"></a>vue-loader</h2><p>官方介绍：<a target="_blank" rel="noopener" href="https://vue-loader.vuejs.org/zh/guide/#vue-cli">起步 | Vue Loader (vuejs.org)</a></p>
<p>基本都是按照这个网站的介绍目录，一步一步的看下来，熟悉一下</p>
<p>因为之前有一些关于webpack的理论知识了解了，所以可以简单的理解这里面的代码和思想之类的</p>
<h3 id="简要介绍一下"><a href="#简要介绍一下" class="headerlink" title="简要介绍一下"></a>简要介绍一下</h3><p><strong>vue-loader</strong>就是一个webpack的loader，主要用于处理 <strong>.vue</strong>文件，如里面的template，script，style，同时也可以与其他的一些loader相配合，如scss这些</p>
<h3 id="webpack-配置"><a href="#webpack-配置" class="headerlink" title="webpack 配置"></a>webpack 配置</h3><p>Vue Loader 的配置和其它的 loader 不太一样。除了通过一条规则将 <code>vue-loader</code> 应用到所有扩展名为 <code>.vue</code> 的文件上之外，请确保在你的 webpack 配置中添加 Vue Loader 的插件：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// webpack.config.js</span></span><br><span class="line"><span class="keyword">const</span> &#123; VueLoaderPlugin &#125; = <span class="built_in">require</span>(<span class="string">&#x27;vue-loader&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  <span class="attr">module</span>: &#123;</span><br><span class="line">    <span class="attr">rules</span>: [</span><br><span class="line">      <span class="comment">// ... 其它规则</span></span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">test</span>: <span class="regexp">/\.vue$/</span>,</span><br><span class="line">        loader: <span class="string">&#x27;vue-loader&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">plugins</span>: [</span><br><span class="line">    <span class="comment">// 请确保引入这个插件！</span></span><br><span class="line">    <span class="keyword">new</span> VueLoaderPlugin()</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>这个插件是必须的！</strong> 它的职责是将你定义过的其它规则复制并应用到 <code>.vue</code> 文件里相应语言的块。例如，如果你有一条匹配 <code>/\.js$/</code> 的规则，那么它会应用到 <code>.vue</code> 文件里的 <code>&lt;script&gt;</code> 块。</p>
<p>一个更完整的 webpack 配置示例看起来像这样：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// webpack.config.js</span></span><br><span class="line"><span class="keyword">const</span> &#123; VueLoaderPlugin &#125; = <span class="built_in">require</span>(<span class="string">&#x27;vue-loader&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  <span class="attr">mode</span>: <span class="string">&#x27;development&#x27;</span>,</span><br><span class="line">  <span class="attr">module</span>: &#123;</span><br><span class="line">    <span class="attr">rules</span>: [</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">test</span>: <span class="regexp">/\.vue$/</span>,</span><br><span class="line">        loader: <span class="string">&#x27;vue-loader&#x27;</span></span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="comment">// 它会应用到普通的 `.js` 文件</span></span><br><span class="line">      <span class="comment">// 以及 `.vue` 文件中的 `&lt;script&gt;` 块</span></span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">test</span>: <span class="regexp">/\.js$/</span>,</span><br><span class="line">        loader: <span class="string">&#x27;babel-loader&#x27;</span></span><br><span class="line">      &#125;,</span><br><span class="line">      <span class="comment">// 它会应用到普通的 `.css` 文件</span></span><br><span class="line">      <span class="comment">// 以及 `.vue` 文件中的 `&lt;style&gt;` 块</span></span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">test</span>: <span class="regexp">/\.css$/</span>,</span><br><span class="line">        use: [</span><br><span class="line">          <span class="string">&#x27;vue-style-loader&#x27;</span>,</span><br><span class="line">          <span class="string">&#x27;css-loader&#x27;</span></span><br><span class="line">        ]</span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">plugins</span>: [</span><br><span class="line">    <span class="comment">// 请确保引入这个插件来施展魔法</span></span><br><span class="line">    <span class="keyword">new</span> VueLoaderPlugin()</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="url会被转换"><a href="#url会被转换" class="headerlink" title="url会被转换"></a>url会被转换</h3><h4 id="例如，下面的模板代码片段："><a href="#例如，下面的模板代码片段：" class="headerlink" title="例如，下面的模板代码片段："></a>例如，下面的模板代码片段：</h4><figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;img src=&quot;../image.png&quot;&gt;</span><br></pre></td></tr></table></figure>

<p>将会被编译成为：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">createElement(<span class="string">&#x27;img&#x27;</span>, &#123;</span><br><span class="line">  <span class="attr">attrs</span>: &#123;</span><br><span class="line">    <span class="attr">src</span>: <span class="built_in">require</span>(<span class="string">&#x27;../image.png&#x27;</span>) <span class="comment">// 现在这是一个模块的请求了</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="为什么"><a href="#为什么" class="headerlink" title="为什么"></a>为什么</h4><p>转换资源 URL 的好处是：</p>
<ol>
<li><code>file-loader</code> 可以指定要复制和放置资源文件的位置，以及如何使用版本哈希命名以获得更好的缓存。此外，这意味着 <strong>你可以就近管理图片文件，可以使用相对路径而不用担心部署时 URL 的问题</strong>。使用正确的配置，webpack 将会在打包输出中自动重写文件路径为正确的 URL。</li>
<li><code>url-loader</code> 允许你有条件地将文件转换为内联的 base-64 URL (当文件小于给定的阈值)，这会减少小文件的 HTTP 请求数。如果文件大于该阈值，会自动的交给 <code>file-loader</code> 处理。</li>
</ol>
<h3 id="使用预处理器"><a href="#使用预处理器" class="headerlink" title="使用预处理器"></a>使用预处理器</h3><p>在 webpack 中，所有的预处理器需要匹配对应的 loader。Vue Loader 允许你使用其它 webpack loader 处理 Vue 组件的某一部分。它会根据 <code>lang</code> 特性以及你 webpack 配置中的规则自动推断出要使用的 loader。</p>
<h4 id="Sass"><a href="#Sass" class="headerlink" title="#Sass"></a><a target="_blank" rel="noopener" href="https://vue-loader.vuejs.org/zh/guide/pre-processors.html#sass">#</a>Sass</h4><p>例如，为了通过 Sass/SCSS 编译我们的 <code>&lt;style&gt;</code> 标签：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm install -D sass-loader node-sass</span><br></pre></td></tr></table></figure>

<p>在你的 webpack 配置中：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  <span class="attr">module</span>: &#123;</span><br><span class="line">    <span class="attr">rules</span>: [</span><br><span class="line">      <span class="comment">// ... 忽略其它规则</span></span><br><span class="line"></span><br><span class="line">      <span class="comment">// 普通的 `.scss` 文件和 `*.vue` 文件中的</span></span><br><span class="line">      <span class="comment">// `&lt;style lang=&quot;scss&quot;&gt;` 块都应用它</span></span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">test</span>: <span class="regexp">/\.scss$/</span>,</span><br><span class="line">        use: [</span><br><span class="line">          <span class="string">&#x27;vue-style-loader&#x27;</span>,</span><br><span class="line">          <span class="string">&#x27;css-loader&#x27;</span>,</span><br><span class="line">          <span class="string">&#x27;sass-loader&#x27;</span></span><br><span class="line">        ]</span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="comment">// 插件忽略</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>现在，除了能够 <code>import &#39;style.scss&#39;</code>，我们还可以在 Vue 组件中使用 SCSS：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">lang</span>=<span class="string">&quot;scss&quot;</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css"><span class="comment">/* 在这里撰写 SCSS */</span></span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>这个块里的任何内容都会被 webpack 当作在一个 <code>*.scss</code> 文件中一样被处理。</p>
<h4 id="PostCSS"><a href="#PostCSS" class="headerlink" title="PostCSS"></a>PostCSS</h4><p>TIP</p>
<p>Vue Loader v15 不再默认应用 PostCSS 变换。你需要通过 <code>postcss-loader</code> 使用 PostCSS。</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm install -D postcss-loader</span><br><span class="line">// webpack.config.js -&gt; module.rules</span><br><span class="line">&#123;</span><br><span class="line">  <span class="built_in">test</span>: /\.css$/,</span><br><span class="line">  use: [</span><br><span class="line">    <span class="string">&#x27;vue-style-loader&#x27;</span>,</span><br><span class="line">    &#123;</span><br><span class="line">      loader: <span class="string">&#x27;css-loader&#x27;</span>,</span><br><span class="line">      options: &#123; importLoaders: 1 &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&#x27;postcss-loader&#x27;</span></span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>PostCSS 的配置可以通过 <code>postcss.config.js</code> 或 <code>postcss-loader</code> 选项来完成。其更多细节请查阅 <a target="_blank" rel="noopener" href="https://github.com/postcss/postcss-loader">postcss-loader 文档</a>。</p>
<p><code>postcss-loader</code> 也可以和上述其它预处理器结合使用。</p>
<h4 id="Babel"><a href="#Babel" class="headerlink" title="#Babel"></a><a target="_blank" rel="noopener" href="https://vue-loader.vuejs.org/zh/guide/pre-processors.html#babel">#</a>Babel</h4><figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">npm install -D babel-core babel-loader</span><br><span class="line">// webpack.config.js -&gt; module.rules</span><br><span class="line">&#123;</span><br><span class="line">  <span class="built_in">test</span>: /\.js?$/,</span><br><span class="line">  loader: <span class="string">&#x27;babel-loader&#x27;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>Babel 的配置可以通过 <code>.babelrc</code> 或 <code>babel-loader</code> 选项来完成</p>
<p>还有一些其他的，如ts这些都可以配置</p>
<h3 id="Scoped-CSS"><a href="#Scoped-CSS" class="headerlink" title="Scoped CSS"></a>Scoped CSS</h3><p>当 <code>&lt;style&gt;</code> 标签有 <code>scoped</code> 属性时，它的 CSS 只作用于当前组件中的元素。这类似于 Shadow DOM 中的样式封装。它有一些注意事项，但不需要任何 polyfill。它通过使用 PostCSS 来实现以下转换：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span> <span class="attr">scoped</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css"><span class="selector-class">.example</span> &#123;</span></span><br><span class="line"><span class="css">  <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="css">&#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;example&quot;</span>&gt;</span>hi<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>转换结果：</p>
<figure class="highlight html"><table><tr><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="css"></span></span><br><span class="line"><span class="css"><span class="selector-class">.example</span><span class="selector-attr">[data-v-f3f3eg9]</span> &#123;</span></span><br><span class="line"><span class="css">  <span class="attribute">color</span>: red;</span></span><br><span class="line"><span class="css">&#125;</span></span><br><span class="line"><span class="css"></span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;example&quot;</span> <span class="attr">data-v-f3f3eg9</span>&gt;</span>hi<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">template</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="子组件的根元素"><a href="#子组件的根元素" class="headerlink" title="子组件的根元素"></a>子组件的根元素</h4><p>使用 <code>scoped</code> 后，父组件的样式将不会渗透到子组件中。不过一个子组件的根节点会同时受其父组件的 scoped CSS 和子组件的 scoped CSS 的影响。这样设计是为了让父组件可以从布局的角度出发，调整其子组件根元素的样式。</p>
<h3 id="CSS-Modules"><a href="#CSS-Modules" class="headerlink" title="CSS Modules"></a>CSS Modules</h3><p>这个感觉跟css in js是未来两大方向了</p>
<p><a target="_blank" rel="noopener" href="https://www.ruanyifeng.com/blog/2016/06/css_modules.html">介绍</a></p>
<h3 id="热重载"><a href="#热重载" class="headerlink" title="热重载"></a>热重载</h3><p>“热重载”不只是当你修改文件的时候简单重新加载页面。启用热重载后，当你修改 <code>.vue</code> 文件时，该组件的所有实例将在<strong>不刷新页面</strong>的情况下被替换。它甚至保持了应用程序和被替换组件的当前状态！</p>
<h4 id="状态保留规则"><a href="#状态保留规则" class="headerlink" title="状态保留规则"></a>状态保留规则</h4><ul>
<li><p>当编辑一个组件的 <code>&lt;template&gt;</code> 时，这个组件实例将就地重新渲染，并保留当前所有的私有状态。能够做到这一点是因为模板被编译成了新的无副作用的渲染函数。</p>
</li>
<li><p>当编辑一个组件的 <code>&lt;script&gt;</code> 时，这个组件实例将<strong>就地销毁并重新创建</strong>。(应用中其它组件的状态将会被保留) 是因为 <code>&lt;script&gt;</code> 可能包含带有副作用的生命周期钩子，所以将重新渲染替换为重新加载是必须的，这样做可以确保组件行为的一致性。这也意味着，如果你的组件带有全局副作用，则整个页面将会被重新加载。</p>
</li>
<li><style> 会通过 vue-style-loader 自行热重载，所以它不会影响应用的状态。</li>
</ul>
<h3 id="函数式组件"><a href="#函数式组件" class="headerlink" title="函数式组件"></a>函数式组件</h3><p>感觉新东西</p>
<p>在一个 <code>*.vue</code> 文件中以单文件形式定义的函数式组件，现在对于模板编译、scoped CSS 和热重载也有了良好的支持。</p>
<p>要声明一个应该编译为函数式组件的模板，请将 <code>functional</code> 特性添加到模板块中。这样做以后就可以省略 <code>&lt;script&gt;</code> 块中的 <code>functional</code> 选项。</p>
<p>模板中的表达式会在<a target="_blank" rel="noopener" href="https://cn.vuejs.org/v2/guide/render-function.html#%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BB%84%E4%BB%B6">函数式渲染上下文</a>中求值。这意味着在模板中，prop 需要以 <code>props.xxx</code> 的形式访问：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template functional&gt;</span><br><span class="line">  &lt;div&gt;&#123;&#123; props.foo &#125;&#125;&lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br></pre></td></tr></table></figure>

<p>你可以在 <code>parent</code> 上访问 <code>Vue.prototype</code> 全局定义的属性：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">&lt;template functional&gt;</span><br><span class="line">  &lt;div&gt;&#123;&#123; parent.$someProperty &#125;&#125;&lt;/div&gt;</span><br><span class="line">&lt;/template&gt;</span><br></pre></td></tr></table></figure>



<h3 id="自定义块"><a href="#自定义块" class="headerlink" title="自定义块"></a>自定义块</h3><p>在 <code>.vue</code> 文件中，你可以自定义语言块。应用于一个自定义块的 loader 是基于这个块的 <code>lang</code> 特性、块的标签名以及你的 webpack 配置进行匹配的。</p>
<p>如果指定了一个 <code>lang</code> 特性，则这个自定义块将会作为一个带有该 <code>lang</code> 扩展名的文件进行匹配。</p>
<p>你也可以使用 <code>resourceQuery</code> 来为一个没有 <code>lang</code> 的自定义块匹配一条规则。例如为了匹配自定义块 <code>&lt;foo&gt;</code>：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">module</span>: &#123;</span><br><span class="line">    <span class="attr">rules</span>: [</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="attr">resourceQuery</span>: <span class="regexp">/blockType=foo/</span>,</span><br><span class="line">        loader: <span class="string">&#x27;loader-to-use&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果找到了一个自定义块的匹配规则，它将会被处理，否则该自定义块会被默默忽略。</p>
<p>此外，如果这个自定义块被所有匹配的 loader 处理之后导出一个函数作为最终结果，则这个 <code>*.vue</code> 文件的组件会作为一个参数被这个函数调用。</p>
<p>然后看完了，感觉跟什么都没看一模一样，哎</p>
</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2022/03/03/vue-loader简单学习/">https://chengsong.info/2022/03/03/vue-loader简单学习/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/vue-loader/">vue loader</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2022/03/03/vite%E5%88%9D%E5%AD%A6%E4%B9%A0/"><i class="fa fa-chevron-left">  </i><span>vite初学习</span></a></div><div class="next-post pull-right"><a href="/2021/11/19/umijs+dva%E7%AE%80%E5%8D%95%E5%AD%A6%E4%B9%A0/"><span>umijs+dva</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>